using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;

namespace DAL
{
    class EntityControl<T>
    {
        private static EntityControl<T> entity;
        private string _AssemblyName;
        static readonly object padlock = new object();

        public static EntityControl<T> CreateEntityControl(string AssemblyName)
        {
            if (entity == null)
            {
                lock (padlock)
                {
                    if (entity == null)
                    {
                        entity = new EntityControl<T>();
                        entity._AssemblyName = AssemblyName;
                    }
                }
            }
            return entity;
        }

        public object AddEntity(Object entity)
        {
            ISession session = SessionFactory.OpenSession(_AssemblyName);
            ITransaction transaction = session.BeginTransaction();
            try
            {
                object ID = session.Save(entity);
                transaction.Commit();
                return ID;
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                throw ex;
            }
            finally
            {
                session.Close();
            }
        }

        public void UpdateEntity(Object entity, Object key)
        {
            ISession session = SessionFactory.OpenSession(_AssemblyName);
            ITransaction transaction = session.BeginTransaction();
            try
            {
                session.Update(entity, key);
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                throw ex;
            }
            finally
            {
                session.Close();
            }
        }

        public void DeleteEntity(object entity)
        {
            ISession session = SessionFactory.OpenSession(_AssemblyName);
            ITransaction transaction = session.BeginTransaction();
            try
            {
                session.Delete(entity);
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                throw ex;
            }
            finally
            {
                session.Close();
            }
        }

        public IList<T> GetEntities(string strHQL,System.Collections.Generic.List<HQLParameter> hp)
        {
            IList<T> lst;
            ISession session = SessionFactory.OpenSession(_AssemblyName);
            //ITransaction transaction = session.BeginTransaction();
            IQuery q = session.CreateQuery(strHQL);
            HQLParameterHelper.CreateHQLParameters(ref q, hp);
            lst = q.List<T>();
            //transaction.Commit();
            session.Close();
            return lst;            
        }

        public ISession GetSessionInterface()
        {
            return SessionFactory.OpenSession(_AssemblyName);
        }

        public long Count()
        {
            string strHQL = String.Format("select count(c) from {0} c",typeof(T).Name);
            System.Collections.Generic.List<HQLParameter> hp = new System.Collections.Generic.List<HQLParameter>(0);
            IList<object> lst;
            ISession session = SessionFactory.OpenSession(_AssemblyName);
            ITransaction transaction = session.BeginTransaction();
            IQuery q = session.CreateQuery(strHQL);
            HQLParameterHelper.CreateHQLParameters(ref q, hp);
            lst = q.List<object>();
            transaction.Commit();
            session.Close();
            return (long)lst[0];
        }

        public IList<T> ALLRow()
        {
            string strHQL = String.Format("from {0} r", typeof(T).Name);
            System.Collections.Generic.List<HQLParameter> hp = new System.Collections.Generic.List<HQLParameter>(0);
            return GetEntities(strHQL, hp);
        }
    }
}
